package com.link.base.base.urlinfo.controller;

import com.link.base.base.urlinfo.model.UrlEncrypt;
import com.link.base.base.urlinfo.model.UrlInfo;
import com.link.base.base.urlinfo.service.UrlInfoService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * url管理页面Controller ,增加注释
 *
 * @author 黄龙建
 * @date 2020/1/8 4:45 下午
 */
@Controller
@RequestMapping("/link/urlInfo")
public class UrlInfoController extends BasicController<UrlInfo> {

    @Resource
    private UrlInfoService urlInfoService;

    @Override
    public BasicService<UrlInfo> getBasicService() throws Exception {
        return urlInfoService;
    }

    @RequestMapping("/updateUrl")
    public Map<String, Object> updateUrl() {
        Map<String, Object> result = new HashMap<>(16);
        try {

            urlInfoService.updateUrl();
            // 处理完成的回调方法，无论是超时还是处理成功，都会进入这个回调方法
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>更新redis中的URL信息</p>
     *
     * @param request
     * @return map
     * @author yrf
     */
    @ResponseBody
    @RequestMapping("updateMapping")
    public Map<String, Object> updateRedisCache(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            urlInfoService.loadUrlToRedis();
            result.put("success", true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * url加密
     * @author LiQun
     * @date 2018/12/13
     */
    @RequestMapping("encrypt")
    public Map<String, Object> encrypt(@RequestBody UrlEncrypt urlEncrypt, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            String encryptData = null;
            // 加密
            encryptData = urlInfoService.encryptUrl(urlEncrypt);
            result.put("encryptData",encryptData);
            result.put("success",true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * url解密
     * @author LiQun
     * @date 2018/12/13
     */
    @RequestMapping("decrypt")
    public  Map<String, Object> decrypt(@RequestBody UrlEncrypt urlEncrypt, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            String urlParams = null;
            // 参数解密
            urlParams = urlInfoService.decryptUrl(urlEncrypt);
            // 获取url中的参数
            result = urlInfoService.getUrlParams(urlParams);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * <p>把默认的url信息写入到全局变量里面去</p>
     *
     * @param request
     * @return map
     * @author wangdawei
     */
    @ResponseBody
    @RequestMapping("setDefUrlCache")
    public Map<String, Object> setDefUrlCache(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            urlInfoService.setDefUrlCache();
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
